<?php

namespace app\admin\model\groupon\store;

use think\Model;

class TakeLog extends Model
{


    // 表名
    protected $name = 'groupon_store_take_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'take_event_text',
        'price_type_text',
        'status_text',
    ];

    const TAKE_STATUS_NO = 0;         // 未结算
    const TAKE_STATUS_OK = 1;         // 已结算
    const TAKE_STATUS_BACK = -1;         // 已退回

    // 商品结算方式
    const TAKE_TYPE_PAY_PRICE = 'pay_price';      // 实际支付价
    const TAKE_TYPE_GOODS_PRICE = 'goods_price';    // 商品价

    // 抽成结算方式
    const TAKE_EVENT_PAYED = 'payed';             // 支付后结算
    const TAKE_EVENT_CONFIRM = 'confirm';         // 确认收货后结算
    const TAKE_EVENT_FINISH = 'finish';           // 订单完成结算
    const TAKE_EVENT_ADMIN = 'admin';             // 手动打款

    public function getStatusTextAttr($value, $data)
    {
        $status_name = '';

        switch ($data['status']) {
            case self::TAKE_STATUS_NO:
                $status_name = '未结算';
                break;
            case self::TAKE_STATUS_OK:
                $status_name = '已结算';
                break;
            case self::TAKE_STATUS_BACK:
                $status_name = '已退回';
                break;
        }

        return $status_name;
    }

    public function getTakeEventTextAttr($value, $data)
    {
        $event_name = '';

        switch ($data['take_event']) {
            case self::TAKE_EVENT_PAYED:
                $event_name = '支付后结算';
                break;
            case self::TAKE_EVENT_CONFIRM:
                $event_name = '自提后结算';
                break;
            case self::TAKE_EVENT_FINISH:
                $event_name = '订单完成结算';
                break;
            case self::TAKE_EVENT_ADMIN:
                $event_name = '手动打款';
                break;
        }

        return $event_name;
    }
    

    public function getPriceTypeTextAttr($value, $data)
    {
        $price_type = '';

        switch ($data['price_type']) {
            case self::TAKE_TYPE_PAY_PRICE:
                $price_type = '实际支付价结算';
                break;
            case self::TAKE_TYPE_GOODS_PRICE:
                $price_type = '商品价结算';
                break;
        }

        return $price_type;
    }

    
    public function user() {
        return $this->belongsTo(\app\admin\model\User::class, 'user_id');
    }


    public function buyer() {
        return $this->belongsTo(\app\admin\model\User::class, 'buyer_id');
    }
    
    public function order() {
        return $this->belongsTo(\app\admin\model\groupon\order\Order::class, 'order_id');
    }
    
    public function item() {
        return $this->belongsTo(\app\admin\model\groupon\order\OrderItem::class, 'order_item_id');
    }


    public function store() {
        return $this->belongsTo(\app\admin\model\groupon\store\Store::class, 'store_id');
    }
    

}
